<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/12/26
 * Time: 10:40
 */

namespace Admin\Controller;


class TypeController extends CommenController
{

	public function index(){
		$this->type = M('type')->select();//分配变量
		$this->display();
	}

	/**
	 * 类型添加
	 */
	public function add(){
		if(IS_POST){
			if(M('type')->add($_POST)){
				$this->success('添加商品类型成功', 'index');
				die;
			}else{
				$this->error('添加商品类型失败');
				die;
			}
		}
		$this->display();
	}
	/**
	 * 类型编辑
	 */
	public function edit(){

		$tid = I('get.tid');
//		$this->p($tid);
		$oldData = M('type')->where(['tid'=>$tid])->find();
//		$this->p($oldData);
		$this->oldData = $oldData;//分配变量
		if (IS_POST){
			if(M('type')->where(['tid'=>$tid])){
				$data =[
					'tid'=>$tid,
					'tname'=>$_POST['tname']
				];
				M('type')->save($data);
				$this->success('修改成功', U('index'));
				die;
			}else{
				$this->error('修改失败');
				die;
			}

		}
		$this->display();

	}
	/**
	 * 类型删除
	 */
	public function del(){
		$tid = I('get.tid');
//		$ccs = M('type')->where('tid','=',$tid)->find();
//		$this->p($ccs);
		if(M('type')->delete($tid)){
			$this->success('删除商品类型成功');
			die;
		}else{
			$this->error('删除商品类型失败');
			die;
		}

	}

}